package com.rtx.edi.platform.custom.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.rtx.edi.gatway.listener.EdiListener;
import com.rtx.edi.model.response.EdiResponse;
import com.rtx.edi.platform.custom.bean.EdiCustomProcess;
import com.rtx.edi.platform.custom.service.EdiCustomProcessService;
import com.rtx.edi.platform.rule.bean.TargetFieldConvertRule;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

@SuppressWarnings("all")
@Api(tags = {"自定义逻辑配置"})
@RestController
@RequestMapping("/v1/ediCustomProcess")
public class EdiCustomProcessController {

	@Autowired
	EdiCustomProcessService ediCustomProcessService;
	
	@Autowired
	ApplicationContext context;
	
	
	
    @ApiOperation(value = "获取EDI自定义逻辑springbean名称", notes = "获取EDI自定义逻辑springbean名称")
    @PostMapping("/queryEdiCustomProcess")
    public EdiResponse queryActiveWhseid(@RequestBody EdiCustomProcess dto){
    	
    	String[] names = context.getBeanNamesForType(EdiListener.class);
    	
    	List<Map<String,String>> list = new ArrayList<Map<String,String>>();
		Map<String,String> datasource = new HashMap<String, String>();
		datasource.put("label", "");
		datasource.put("value", "");
		list.add(datasource);
		
		for(String name:names) {
			Map<String,String> temp = new HashMap<String, String>();
			temp.put("label", name);
			temp.put("value", name);
			list.add(temp);
		}
		
        return EdiResponse.success().data("item", list);
    }
	
    /**
     * 获取表配置
     * @return
     */
    @ApiOperation(value = "自定义逻辑配置", notes = "自定义逻辑配置")
    @PostMapping("selectEdiCustomProcessList")
    public EdiResponse selectEdiTableConfigList(@RequestBody EdiCustomProcess dto){
        Page<EdiCustomProcess> pageParam = new Page<>(dto.getPageNum(), dto.getPageSize());
        ediCustomProcessService.selectList(pageParam,dto);
        return EdiResponse.success().data("page",pageParam);
    }
    
    
    /**
     * 查询表配置
     * @return
     */
    @ApiOperation(value = "自定义逻辑配置", notes = "自定义逻辑配置")
    @PostMapping("selectEdiCustomProcessById")
    public EdiResponse selectEdiCustomProcessId(@RequestBody EdiCustomProcess dto){
        return EdiResponse.success().data("item",ediCustomProcessService.getById(dto.getId()));
    }

    /**
     * 新增表配置
     * @return
     */
    @ApiOperation(value = "自定义逻辑配置", notes = "自定义逻辑配置")
    @PostMapping("addEdiCustomProcess")
    public EdiResponse addEdiCustomProcess(@RequestBody EdiCustomProcess dto){
    	try {
    		dto.setId(null);
    		ediCustomProcessService.save(dto);
            return EdiResponse.success();
		} catch (Exception e) {
			e.printStackTrace();
			return EdiResponse.failed().returnMgr(e.getMessage());
		}
        
    }

    /**
     * 修改表配置
     * @return
     */
    @ApiOperation(value = "自定义逻辑配置", notes = "自定义逻辑配置")
    @PostMapping("editEdiCustomProcess")
    public EdiResponse editEdiCustomProcess(@RequestBody EdiCustomProcess dto){
    	ediCustomProcessService.updateById(dto);
        return  EdiResponse.success();
    }
    
    /**
     * 批量删除Edi开放接口创建配置
     * @return
     */
    @ApiOperation(value = "自定义逻辑配置", notes = "自定义逻辑配置")
    @PostMapping("deleteEdiCustomProcess")
    public EdiResponse deleteEdiCustomProcessByIds(@RequestBody List<EdiCustomProcess> list){
        List<Long> ids = list.stream().map(p -> p.getId()).distinct().collect(Collectors.toList());
        return  ediCustomProcessService.removeBatchByIds(ids) ? EdiResponse.success() : EdiResponse.failed();
    }

}
